/**
 * Created by thinkpad on 2017-05-31.
 */

XBox.define("form.api", {
    title: "XBox.form API",

    templates: {
        main: ['<div>' +
        '   <div class="menu">' +
        '   <div style="border-bottom: 1px solid #f88;padding: 3px;color: red;">由于种种原因，如果API说明与实际代码有差异，一切效果以实际代码为准。</div>' +
        '       <ul class="apimenu">' +
        '           <% for(var i=0; i < data.length; i++) { %>' +
        '               <li class="menuitem">' +
        '                   <a data-url="<%=data[i].url%>"><%= data[i].name %></a>' +
        '               </li>' +
        '           <%}%>' +
        '       </ul>' +
        '   </div>' +
        '   <div class="code"><pre></pre></div>' +
        '</div>']
    },
    elements: {
        menu: ".menu",
        code: ".code>pre",
    },
    delegates: {
        ".menuitem>a": "showConfig"
    },
    mainTplData: function () {
        var menus = [
            {
                name: "form",
                url: "api/form.json"
            }, {
                name: "text",
                url: "api/text.json"
            }, {
                name: "upload",
                url: "api/upload.json"
            }, {
                name: "multiSelect",
                url: "api/multiSelect.json"
            }];
        return menus;
    },

    afterRender: function () {
        this.controls.menu.find("a:first").click();
    },
    showConfig: function (target, e) {
        var that = this;
        this.controls.menu.find("a.active").removeClass("active");
        $(target).addClass("active");
        var url = target.data("url");
        if (url) {
            $.ajax({
                url: url + "?d=" + new Date().getTime(),
                dataType: "text",
                success: function (html) {
                    that.controls.code.html(html);
                }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                    that.controls.code.html(textStatus || errorThrown);
                }
            });
        } else {
            that.controls.code.html("没找到地址");
        }
        return false;
    }
});